package cn.hp.customer.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

// jedis工具类进行jedis对象的创建及获取
public class JedisUtil {

    private static JedisPool jedisPool ;

    // 静态代码块
    static {
        // 加载配置文件  类路径进行加载  拿到配置文件的输入流
        InputStream in = JedisUtil.class.getClassLoader().getResourceAsStream("redis.properties");
        // 读取配置文件
        Properties properties = new Properties();
        // 将配置文件读取进对象中
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // redis连接池  mysql一样 使用pool
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal")));
        poolConfig.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));

        jedisPool = new JedisPool(poolConfig, properties.getProperty("host"),Integer.parseInt(properties.getProperty("port")) );

    }

    // 获取jedis对象
    public static Jedis getJedis(){
        return jedisPool.getResource() ;
    }



}
